1903F - Babysitting - CodeForces Solution


2-sat binary search data structures graphs trees *2500

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops")
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
const ll ILL=2167167167167167167;
const int INF=2100000000;
const int mod=998244353;
#define rep(i,a,b) for (int i=(int)(a);i<(int)(b);i++)
#define all(p) p.begin(),p.end()
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
void yneos(bool a,bool upp=0){if(a) cout<<(upp?"YES\n":"Yes\n"); else cout<<(upp?"NO\n":"No\n");}
template<class T> void vec_out(vector<T> &p,int ty=0){
if(ty==2){cout<<'{';for(int i=0;i<(int)p.size();i++){if(i){cout<<",";}cout<<'"'<<p[i]<<'"';}cout<<"}\n";}
else{if(ty==1){cout<<p.size()<<"\n";}for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout<<p[i];}cout<<"\n";}}
template<class T> T vec_min(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmin(ans,x);return ans;}
template<class T> T vec_max(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmax(ans,x);return ans;}
template<class T> T vec_sum(vector<T> &a){assert(!a.empty());T ans=a[0]-a[0];for(auto &x:a) ans+=x;return ans;}
int pop_count(long long a){int res=0;while(a){res+=(a&1),a>>=1;}return res;}

namespace po167{
struct SCC{
	std::vector<std::vector<int>> G;
	std::vector<std::vector<int>> G_rev;
	int size;
	SCC(int n):G(n),G_rev(n),size(n){}
	void clear(){
		for(auto &x:G) x.clear();
		for(auto &x:G_rev) x.clear();
	}
	void reserve(int ind,size_t n_){
		G[ind].reserve(n_);
		G_rev[ind].reserve(n_);
	}
	int add_ver(){
		G.push_back({});
		G_rev.push_back({});
		size++;
		return size-1;
	}
	void add_edge(int from,int to){
		assert(0<=to&&to<size);
		assert(0<=from&&from<size);
		G[from].push_back(to);
		G_rev[to].push_back(from);
	}

	void dfs1(std::vector<int> &order,std::vector<int> &seen,int ind,int &now){
		if(seen[ind]!=0) return;
		seen[ind]=-1;
		for(auto x:G[ind]){
			dfs1(order,seen,x,now);
		}
		order[now]=ind;
		now++;
	}
	void dfs2(std::vector<int> &seen,int ind,int rank){
		if(seen[ind]!=-1) return;
		seen[ind]=rank;
		for(auto x:G_rev[ind]){
			dfs2(seen,x,rank);
		}
	}
	std::vector<std::vector<int>> tp_sort(){
		std::vector<int> seen(size,0),order(size);
		int now=0;
		for(int i=0;i<size;i++) dfs1(order,seen,i,now);
		now=0;
		for(int i=size-1;i>=0;i--){
			if(seen[order[i]]==-1){
				dfs2(seen,order[i],now);
				now++;
			}
		}
		std::vector<std::vector<int>> ans(now);
		for(int i=0;i<size;i++){
			ans[seen[i]].push_back(i);
		}
		return ans;
	}
	std::vector<int> for_two_sat(){
		std::vector<int> seen(size,0),order(size);
		int now=0;
		for(int i=0;i<size;i++) dfs1(order,seen,i,now);
		now=0;
		for(int i=size-1;i>=0;i--){
			if(seen[order[i]]==-1){
				dfs2(seen,order[i],now);
				now++;
			}
		}
		return seen;
	}
};

struct two_SAT{
	po167::SCC graph;
	std::vector<bool> ans;
	bool exists=false;
	int size;
	two_SAT(int n):graph(2*n),ans(n),size(n){}
	void add_clause(int i,bool f,int j,bool g){
		graph.add_edge(i*2+(!f),j*2+(g));
		graph.add_edge(j*2+(!g),i*2+(f));
	}
	void clear(){
		graph.clear();
		exists=false;
	}
	void one_true_in_group(std::vector<std::pair<int,int>> &p){
		int ind;
		for(int i=0;i<(int)(p.size())-1;i++){
			ind=graph.add_ver();
			graph.add_ver();
			ind/=2;
			add_clause(ind,1,p[i].first,p[i].second^1);
			add_clause(ind,0,p[i+1].first,p[i+1].second^1);
			if(i){
				add_clause(ind-1,0,ind,1);
			}
			ans.push_back(false);
			size++;
		}
	}
	bool build(){
		auto rank=graph.for_two_sat();
		exists=true;
		for(int i=0;i<size;i++){
			if(rank[i*2]==rank[i*2+1]){
				exists=false;
				break;
			}
			ans[i]=(rank[i*2]<rank[i*2+1]);
		}
		return exists;
	}
};
}
using po167::SCC;
using po167::two_SAT;


void solve();
// oddloop
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int t=1;
	cin>>t;
	rep(i,0,t) solve();
}

void solve(){
	int N,M;
	cin>>N>>M;
	vector<int> A(M),B(M);
	rep(i,0,M) cin>>A[i]>>B[i],A[i]--,B[i]--;
	int L=1,R=N+1;
	int size=1,d=0;
	while(size<N) d++,size*=2;
	vector<int> C(size);
	rep(i,0,M){
		C[A[i]]++;C[B[i]]++;
	}
	two_SAT T(size*2);
	rep(i,0,d){
		rep(j,1<<i,1<<(i+1)){
			T.graph.reserve(j,(j-(1<<i)?(size>>i):0)+3);
		}
	}
	rep(i,0,size){
		T.graph.reserve(i+size,(i<N?C[i]+d*2+6:2));
	}
	while(R-L>1){
		int mid=(L+R)/2;
		rep(i,0,M){
			T.add_clause(A[i]+size,1,B[i]+size,1);
		}
		rep(i,1,size){
			T.add_clause(i,1,i*2,0);
			T.add_clause(i,1,i*2+1,0);
		}
		rep(i,0,N){
			int l=i+1,r=min(N,i+mid);
			l+=size,r+=size;
			while(r-l){
				if(l&1){
					T.add_clause(i+size,0,l++,0);
				}
				if(r&1){
					T.add_clause(i+size,0,--r,0);
				}
				l>>=1;r>>=1;
			}
		}
		if(T.build()) L=mid;
		else R=mid;
		T.clear();
	}
	cout<<L<<"\n";
}


Comments

Submit
0 Comments
More Questions

101. Symmetric Tree
77. Combinations
46. Permutations
226. Invert Binary Tree
112. Path Sum
1556A - A Variety of Operations
136. Single Number
169. Majority Element
119. Pascal's Triangle II
409. Longest Palindrome
1574A - Regular Bracket Sequences
1574B - Combinatorics Homework
1567A - Domino Disaster
1593A - Elections
1607A - Linear Keyboard
EQUALCOIN Equal Coins
XOREQN Xor Equation
MAKEPAL Weird Palindrome Making
HILLSEQ Hill Sequence
MAXBRIDGE Maximise the bridges
WLDRPL Wildcard Replacement
1221. Split a String in Balanced Strings
1002. Find Common Characters
1602A - Two Subsequences
1555A - PizzaForces
1607B - Odd Grasshopper
1084A - The Fair Nut and Elevator
1440B - Sum of Medians
1032A - Kitchen Utensils
1501B - Napoleon Cake